import { baseUrl } from './config.js';
import $store from '@/store/index.js';

export default function(options = {}) {
	let url = `${baseUrl}/ajax/upload`;
	let header = {
		server: 'true'
	}
	if($store.state.$token){
		header['ba-user-token'] = $store.state.$token;
	}
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: url,
			filePath: options.filePath,
			name: 'file',
			header: header,
			success: (res) => {
				let statusCode = res.statusCode;
				let resData = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
				let { msg, data } = resData;
				if (statusCode == 200) {
					resolve(data);
				} else {
					uni.hideLoading();
					// 全局提示
					uni.showToast({
						title: msg,
						icon: 'none',
						duration: 2000
					});
				}
			},
			fail: (err) => {
				reject(err);
			}
		})
	})
}

